#!/usr/bin/env python2

import hashlib
import random

import requests

HOST = '128.1.135.165:81'
HOST = 'localhost'

API_USER = '216456dd'
API_KEY = '659fdded-c762-480f-a83b-5c2a11a37bc6'

ORDER_ID = 'O-1540449443-95108600'
PRICE = '1'
REAL_PRICE = '1.00'


def gen_signature(**params):
    """
    api_user
    price
    type
    redirect
    order_id
    order_info
    """
    param_keys = list(params.keys())
    param_keys.sort()
    param_str = API_KEY

    for key in param_keys:
        if isinstance(params[key], unicode):
            param_str += params[key].encode('utf-8')
        else:
            param_str += str(params[key])

    signature = hashlib.md5(param_str).hexdigest()
    return signature


def main():
    url = 'http://%s/e/payapi/ppz/payend.php?act=notify' % HOST
    data = {
        'order_id': ORDER_ID,
        'price': PRICE,
        'ppz_order_id': str(random.randint(60000, 99999)),
        'order_info': '',
        'real_price': REAL_PRICE,
    }
    data['signature'] = gen_signature(**data)
    requests.post(url, data=data)


if __name__ == '__main__':
    main()
